import grace from "../../utils/grace.js"
grace.page({
      data: {
            key_word: '',
            project_name: '',
            latitude: '',
            longitude: '',
            project_list: [],
            key_project_list: [],
            project: {}
      },
      onLoad: function(options) {
            if (options.length > 0) {
                  let params = options[0];
                  if (params) {
                        this.$data.project_name = params[0].project_name;
                        this.$data.latitude = params[0].latitude;
                        this.$data.longitude = params[0].longitude;
                  }
            }
            this.wxLocation();
            wx.hideNavigationBarLoading();
      },
      returnBack: function(e) {
            this.$goBack();
      },
      wxSearchInput: function(e) {
            let key_word = e.detail.value;
            console.log(key_word);
            this.$http.get('api/home/show_project_by_keyword', {
                  key_word: key_word
            }).then((res) => {
                  if (res) {
                        this.$data.key_project_list = res.data;
                  }
            });
      },
      wxLocation: function(res) {
            var _this = this;
            wx.getLocation({
                  type: 'gcj02',
                  success: function(res) {
                        console.log("用户当前位置 success:" + JSON.stringify(res));
                        _this.$http.get('/api/home/get_index_project', {
                              latitude: res.latitude,
                              longitude: res.longitude
                        }).then((res) => {
                              _this.$data.project = res;
                        });
                        _this.$http.get('api/home/show_project_by_keyword', {
                              latitude: res.latitude,
                              longitude: res.longitude
                        }).then((res) => {
                              if (res) {
                                    _this.$data.project_list = res;
                              }
                        });
                  },
                  fail(error) {
                        console.log("用户当前位置 fail:" + JSON.stringify(error));
                        wx.showToast({
                              title: '授权失败，请刷新页面授权..',
                              icon: "none",
                              duration: 1500
                        });
                  }
            })
      },
      choseProject: function(e) {
            console.log(e);
            let _this = this;
            let project_name = e.currentTarget.dataset.project_name;
            let latitude = e.currentTarget.dataset.latitude;
            let longitude = e.currentTarget.dataset.longitude;
            let pages = getCurrentPages(); //获取当前页面的页面栈
            let prevPage = pages[pages.length - 2]; //获取上级页面的page对象
            if (prevPage) {
                  _this.$http.get('/api/home/get_index_project', {
                        latitude: latitude,
                        longitude: longitude,
                        project_name: project_name
                  }).then((res) => {
                        prevPage.wxLocation({
                              project: res
                        })
                  });
                  this.$goBack();
            }
      }
})